﻿using PC_Touch.DataOP;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace PC_Touch
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
            x = this.Width;
            y = this.Height;
            setTag(this);
            this.Resize += new EventHandler(this.Form_Resize);
        }
       
        private void IObutton_Click(object sender, EventArgs e)
        {
            IOMonitorSetting iomonitorSetting = new IOMonitorSetting();
            iomonitorSetting.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            IOMonitor iomonitor = new IOMonitor();
            iomonitor.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            uiNavBar1.SetNodeSymbol(uiNavBar1.Nodes[0], 361875);
            uiNavBar1.SetNodeSymbol(uiNavBar1.Nodes[1], 363013);
            uiNavBar1.SetNodeSymbol(uiNavBar1.Nodes[2], 57442);
            uiNavBar1.SetNodeSymbol(uiNavBar1.Nodes[3], 61451);
            uiNavBar1.SetNodeSymbol(uiNavBar1.Nodes[4], 560066);
            var jsonMachine = JsonSerializer.Deserialize<JsonMachine>(File.ReadAllText(Environment.CurrentDirectory + @"/AppConfig/Machine.Json", Encoding.UTF8));
<<<<<<< HEAD
            string msg = PLCHelper.OpenPLC(jsonMachine.plcIP,103);
=======
            string msg = PLCHelper.OpenPLC(jsonMachine.plcIP);
>>>>>>> 7dcc016849873c89bffdc611d8d2656801e356cd
            if (msg.Contains("success"))
            {
                this.ShowSuccessTip(msg);
            }
            else
            {
                this.ShowErrorTip(msg);
            }
            uiNavBar1.SelectedIndex = 0;
            //监听事件初始化
            PLCHelper.TriggerEventInit();
           // PLC轮询读取触发信号

        }

        private void button3_Click(object sender, EventArgs e)
        {
            TaskManagement taskManagement = new TaskManagement();
            taskManagement.Show();
        }

        #region 控件缩放
        private float x = 1;
        private float y = 1;
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        private void Form_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / x;
            float newy = (this.Height) / y;
            setControls(newx, newy, this);
        }
        #endregion

        private void button4_Click(object sender, EventArgs e)
        {
            CollectionConfig collectionConfig = new CollectionConfig();
            collectionConfig.Show();
        }
        private Dictionary<string, Form> formCache = new Dictionary<string, Form>();

        string lastPages = "";
        private void uiNavBar1_NodeMouseClick(TreeNode node, int menuIndex, int pageIndex)
        {
            if (node.Nodes.Count == 0) 
            {
                Form form;
                if (formCache.TryGetValue(lastPages, out form)) 
                {
                    form.Close();
                }
                //销毁上一个页面
                string formName = (string)node.Tag; // 子窗体的名称标识符
                lastPages = formName;
                Form childForm;
                if (!formCache.TryGetValue(formName, out childForm) || childForm == null || childForm.IsDisposed)
                {
                    switch (formName)
                    {
                        case "IOMonitor":
                            childForm = new IOMonitor();
                            break;
                        case "Form1":
                            childForm = new Form1();
                            break;
                        case "IOMonitorSetting":
                            childForm = new IOMonitorSetting();
                            break;
                        case "TaskManagement":
                            childForm = new TaskManagement();
                            break;
                        case "CollectionConfig":
                            childForm = new CollectionConfig();
                            break;
                        case "SettingsQF":
                            childForm = new SettingsQF();
                            break;
                        case "SettingsQG":
                            childForm = new SettingsQG();
                            break;
                        case "SettingsSF":
                            childForm = new SettingsSF();
                            break;
                        case "SettingsFS":
                            childForm = new SettingsFS();
                            break;
                          case "SettingsParameter":
                            childForm = new SettingsParameter();
                            break;
                        default:
                            break;
                    }
                    formCache[formName] = childForm;
                    for (int i = 0; i < tabControl1.TabPages.Count; i++)
                    {
                        if (tabControl1.TabPages[i].Text == childForm.Text)
                        {
                            tabControl1.TabPages.Remove(tabControl1.TabPages[i]);
                        }
                    }
                  
                    // 创建一个新的 TabPage 并设置其标题
                    TabPage tabPage = new TabPage(childForm.Text);

                    // 将子窗体添加到 TabPage 中
                    childForm.TopLevel = false; // 设置为非顶级窗口
                    childForm.FormBorderStyle = FormBorderStyle.FixedToolWindow; // 移除边框
                    childForm.Dock = DockStyle.Fill; // 使子窗体填满 TabPage
                    tabPage.Controls.Add(childForm);
                    childForm.Show();
                    // 将 TabPage 添加到 TabControl 中
                    tabControl1.TabPages.Add(tabPage);
                    tabControl1.SelectedTab = tabPage;
                }
                else
                {
                    // 如果子窗体已经存在，则激活对应的 TabPage
                    foreach (TabPage tabPage in tabControl1.TabPages)
                    {
                        if (tabPage.Controls.Contains(childForm))
                        {
                            tabControl1.SelectedTab = tabPage;
                            break;
                        }
                    }
                }
            }
        }

        private void timerDateTime_Tick(object sender, EventArgs e)
        {
            uiMarkLabel1.Text = DateTime.Now.ToString("G");
        }

        private void uiMarkLabel5_Click(object sender, EventArgs e)
        {
            if (uiMarkLabel5.Text == "中文")
            {
                uiMarkLabel5.Text = "English";
                LanguageHelper.zh_enFlag = "English";
            }
            else
            {
                uiMarkLabel5.Text = "中文";
                LanguageHelper.zh_enFlag = "中文";
            }
            setZH_EN();
        }
        private void setZH_EN()
        {
            if (LanguageHelper.zh_enFlag == "English")
            {
                uiNavBar1.Nodes[0].Text = "HomePage";
                uiNavBar1.Nodes[1].Text = "ManualPage";
                uiNavBar1.Nodes[1].Nodes[0].Text = "Manual Air & Water Valves";
                uiNavBar1.Nodes[1].Nodes[1].Text = "Cylinder";
                uiNavBar1.Nodes[1].Nodes[2].Text = "ServoMotor";
                uiNavBar1.Nodes[1].Nodes[3].Text = "Fans/lighting/ball valves/insulation film";
                uiNavBar1.Nodes[2].Text = "AlarmPage";
                uiNavBar1.Nodes[2].Nodes[0].Text = "TriggerEvent";
                uiNavBar1.Nodes[2].Nodes[1].Text = "AddressConfiguration";
                uiNavBar1.Nodes[2].Nodes[2].Text = "ViewAlarms";
                uiNavBar1.Nodes[3].Text = "ParameterSettings";
                uiNavBar1.Nodes[4].Text = "I/O Monitor";
                uiNavBar1.Nodes[4].Nodes[0].Text = "PointSetting";
                uiNavBar1.Nodes[4].Nodes[1].Text = "I/O Monitor";
            }
            else 
            {
                uiNavBar1.Nodes[0].Text = "主页画面";
                uiNavBar1.Nodes[1].Text = "手动设置";
                uiNavBar1.Nodes[1].Nodes[0].Text = "手动气阀&水阀";
                uiNavBar1.Nodes[1].Nodes[1].Text = "手动气缸";
                uiNavBar1.Nodes[1].Nodes[2].Text = "伺服电机";
                uiNavBar1.Nodes[1].Nodes[3].Text = "风扇照明/球阀/隔热膜";
                uiNavBar1.Nodes[2].Text = "报警界面";
                uiNavBar1.Nodes[2].Nodes[0].Text = "触发事件";
                uiNavBar1.Nodes[2].Nodes[1].Text = "地址配置";
                uiNavBar1.Nodes[2].Nodes[2].Text = "查看报警";
                uiNavBar1.Nodes[3].Text = "参数设置";
                uiNavBar1.Nodes[4].Text = "I/O监控";
                uiNavBar1.Nodes[4].Nodes[0].Text = "点位设置";
                uiNavBar1.Nodes[4].Nodes[1].Text = "I/O监控";
            }

        }
    }
}
